#coding=utf8
from appium.webdriver.common.appiumby import AppiumBy


class Find_UI_Element:
    def __init__(self,driver):#需传入一个driver对象
        self.driver=driver

    def by_Text(self,text):#通过元素的text属性定位
        temp='text("%s")'%text
        return self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,temp)

    def by_Id(self,id):#通过元素的id属性定位
        temp='resourceId("%s")'%id
        return self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,temp)

    def by_Classname_Text(self,classname,text):#通过元素的class和text属性定位
        temp='className("%s").text("%s")'%(classname,text)
        return self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,temp)

    def by_Id_Text(self,id,text):#通过元素的id和text属性定位
        temp = 'resourceId("%s").text("%s")' % (id, text)
        return self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,temp)

    def by_Brother_TT(self,brother,target):#目标与参考元素都通过text定位
        temp='text("%s").fromParent(text("%s"))'%(brother,target)
        return self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,temp)

    def by_Brother_IT(self,brother,target):#目标元素是id定位，参考元素是text定位
        temp='text("%s").fromParent(resourceId("%s"))'%(brother,target)
        return self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,temp)

    def by_Brother_CT(self,brother,target):#目标元素是class定位，参考元素是text定位
        temp='text("%s").fromParent(className("%s"))'%(brother,target)
        return self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,temp)

    def by_Brother_II(self,brother,target):#目标元素是id定位，参考元素是id定位
        temp='resourceId("%s").fromParent(resourceId("%s"))'%(brother,target)
        return self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,temp)

# driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.ListView").childSelector(new UiSelector().text("Custom View"))').click()  #通过父级元素定位子集元素
    def by_Parent_CT(self,parent,target):#父级元素是class定位，目标元素是text定位
        temp = 'className("%s").childSelector(text("%s"))' % (parent, target)
        return self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,temp)

    def by_Parent_IT(self,parent,target):#父级元素是ID定位，目标元素是text定位
        temp = 'resourceId("%s").childSelector(text("%s"))' % (parent, target)
        return self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,temp)

    def by_Parent_TT(self,parent,target):#父级元素是text定位，目标元素是text定位
        temp = 'text("%s").childSelector(text("%s"))' % (parent, target)
        return self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,temp)

    def by_Parent_II(self,parent,target):#父级元素是id定位，目标元素是id定位
        temp = 'resourceId("%s").childSelector(resourceId("%s"))' % (parent, target)
        return self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,temp)

##############下面是找web元素的方法###################
    def web_By_TagName(self,tagname):
        return self.driver.find_element_by_tag_name(tagname)

    def web_By_Id(self,id):
        return self.driver.find_element_by_id(id)

    def web_By_Xpath(self,xpath):
        return self.driver.find_element_by_xpath(xpath)

    def web_By_LinkText(self,text):
        return self.driver.find_elements_by_link_text(text)

    def web_By_Css(self,css):
        return self.driver.find_element_by_css_selector(css)
#例如：attr_json=[["by_Text",args1,args2...],]by_Text为page层自定义方法

    def find_Element(self,attr_json):
        FUE = Find_UI_Element(self.driver)
        self.ele_obj_dic={}
        for i in attr_json:
            method=getattr(FUE,i[0])
            args=i[1:]
            try:
                last_ele=args[:-2:-1][0]
                self.ele_obj_dic[last_ele]=method(*args)
            except:
                self.ele_obj_dic[last_ele]=None
        ele_obj_list=list(self.ele_obj_dic.values())
        return ele_obj_list[0]








#通用的方法
#     def find_Element(self,attr):
#             attr_temp=attr.split(">")
#             if attr_temp[0]=="y" and attr_temp[1]=="g":
#                 temp=attr_temp[2].split("|")
#                 str1 = temp[0].split("?")
#                 str2 = temp[1].split("?")
#                 if "class"==str1[0] and "text"==str2[0]:#组合定位格式：　class?value | text?value
#                     return self.by_Classname_Text(str1[1], str2[1])
#                 elif "id"==str1[0] and "text"==str2[0]:#组合定位格式：　id?value | text?value
#                     return self.by_Id_Text(str1[1], str2[1])
#
#             elif attr_temp[0]=="y" and attr_temp[1]=="s":
#                 temp=attr_temp[2].split("?")
#                 if temp[0]=="text":
#                     return self.by_Text(temp[1])
#                 elif temp[0]=="id":
#                     return self.by_Id(temp[1])
#
#             elif attr_temp[0]=="y" and attr_temp[1]=="b":
#                 temp = attr_temp[2].split("|")
#                 str1 = temp[0].split("?")
#                 str2 = temp[1].split("?")
#                 if "text"==str1[0] and "text"==str2[0]:  # 同级定位格式：　text?value | text?value
#                     return self.by_Brother_TT(str1[1], str2[1])
#                 elif "text"==str1[0] and "id"==str2[0]:# 同级定位格式：　text?value | id?value
#                     return self.by_Brother_IT(str1[1], str2[1])
#                 elif "text"==str1[0] and "class"==str2[0]:
#                     return self.by_Brother_CT(str1[1], str2[1])# 同级定位格式：　text?value | class?value
#                 elif "id"==str1[0] and "id"==str2[0]:
#                     return self.by_Brother_II(str1[1], str2[1])# 同级定位式：　id?value | id?value
#
#             elif attr_temp[0]=="y" and attr_temp[1]=="p":
#                 temp = attr_temp[2].split("|")
#                 str1 = temp[0].split("?")
#                 str2 = temp[1].split("?")
#                 if "class"==str1[0] and "text"==str2[0]:#父级定位: class?value|text?value
#                     return self.by_Parent_CT(str1[1],str2[1])
#                 elif "id"==str1[0] and "text"==str2[0]:#父级定位: id?value|text?value
#                     return self.by_Parent_IT(str1[1],str2[1])
#                 elif "text"==str1[0] and "text"==str2[0]:#父级定位: text?value|text?value
#                     return self.by_Parent_TT(str1[1],str2[1])
#                 elif "id"==str1[0] and "id"==str2[0]:#父级定位: id?value|id?value
#                     return self.by_Parent_II(str1[1],str2[1])
#
#             elif attr_temp[0]=="h":#  h>> attr:value
#                 temp=attr_temp[2].split("?")
#                 if "xpath"==temp[0]:
#                     return self.web_By_Xpath(temp[1])
#                 elif "id"==temp[0]:
#                     return self.web_By_Id(temp[1])
#                 elif "linktext"==temp[0]:
#                     return self.web_By_LinkText(temp[1])
#                 elif "css"==temp[0]:
#                     return self.web_By_Css(temp[1])
# #要使用uiautomator2
#             # elif attr_temp[0]=="y" and attr_temp[1]=="t":#获取tost元素 y>t>xpath?value
#             #     temp=attr_temp[2].split("?")
#             #     if temp[0]=="xpath":
#             #         self.wait_Element("xpath",temp[1])
#
#             return None







#element_attr格式：原生（y）/web(h)>组合定位（g）/单个属性定位(s)/同级定位（b）/父级定位(p)/tost(t)>元素属性1?值|元素属性2?值(目标元素)


